package com.ewanghuiju.app.util;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.text.TextUtils;

import com.android.tu.loadingdialog.LoadingDailog;
import com.ewanghuiju.app.R;

import java.lang.ref.WeakReference;

import androidx.appcompat.app.AlertDialog;

public class LoadingDialogUtils {
    public LoadingDialogUtils() {
    }

    public static LoadingDailog dialog_ios;

    public static LoadingDailog.Builder loadBuilder;

    private static WeakReference<Activity> reference;

    public static void dismissDialog_ios() {
        if (dialog_ios != null) {
            try {
                dialog_ios.dismiss();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    /**
     * 注销加载框，避免发生内存泄露
     */
    public static void unInit() {
        if (dialog_ios != null) {
            try {
                dismissDialog_ios();
                dialog_ios = null;
                loadBuilder = null;
                reference = null;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 仿iso加载效果
     */
    public static void show(Activity context) {
        show(context, "加载中");
    }

    /**
     * 仿iso加载效果
     */
    public static void show(Activity context, String info) {
        if (loadBuilder == null || dialog_ios == null || reference == null || reference.get() == null || reference.get().isFinishing()) {
            loadBuilder = new LoadingDailog.Builder(context)
                    .setMessage("" + info)
                    .setCancelable(true)
                    .setCancelOutside(true);
            dialog_ios = loadBuilder.create();
        }
        dialog_ios.show();
    }

    private String title;
    private String message;
    private OnClickListener onClickListener;
    private Context context;

    public LoadingDialogUtils(Context context) {
        this.context = context;
    }

    public LoadingDialogUtils title(String t) {
        this.title = t;
        return this;
    }

    public LoadingDialogUtils message(String t) {
        this.message = t;
        return this;
    }

    public LoadingDialogUtils listener(OnClickListener t) {
        this.onClickListener = t;
        return this;
    }

    public void show() {

        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        if (!TextUtils.isEmpty(title)) {
            builder.setTitle(title);
        }

        builder.setMessage(message);
        //点击对话框以外的区域是否让对话框消失
        builder.setCancelable(true);
        //设置正面按钮
        builder.setPositiveButton(R.string.button_sure, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                if (onClickListener != null) {
                    onClickListener.onConfirm();
                }
            }
        });
        //设置反面按钮
        builder.setNegativeButton(R.string.button_quxiao, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        builder.show();
    }

    public interface OnClickListener {
        void onConfirm();
    }
}
